package com.example.service.impl.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.dto.pc.PPRDto;
import com.example.entity.dto.pc.PPRListDto;
import com.example.entity.mysql.Productprocessresource;
import com.example.entity.mysql.Resourcegroup;
import com.example.entity.vo.pc.PPRVo;
import com.example.mapper.pc.ProductprocessresourceMapper;
import com.example.service.pc.ProductprocessresourceService;
import com.example.service.pc.ResourcegroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-05-19
 */
@Service
public class ProductprocessresourceServiceImpl extends ServiceImpl<ProductprocessresourceMapper, Productprocessresource> implements ProductprocessresourceService {

    @Autowired
    private ProductprocessresourceMapper mapper;
    @Autowired
    private ResourcegroupService resourcegroupService;

    @Override
    public List<PPRVo> PPRList(PPRListDto dto) {
        return mapper.PPRList(dto);
    }

    @Override
    public List<Productprocessresource> selPPRList(PPRDto dto) {
        List<Productprocessresource> list = null;
        if(dto.getProductLineResourceId() != null) {
            // 没有的话，就调用没有生产线/机器的
            list = mapper.selPPRList2(dto);
            // 添加用户自定义的生产线/机器
            Resourcegroup one = resourcegroupService.getOne(new LambdaQueryWrapper<Resourcegroup>().eq(Resourcegroup::getResourceGroupId, dto.getProductLineResourceId()));
            if(one != null) {
                list.add(mapper.selectOne(new LambdaQueryWrapper<Productprocessresource>()
                        .eq(Productprocessresource::getResourceGroupId, one.getResourceGroupParentId())
                        .eq(Productprocessresource::getProductId, dto.getProductId())
                        .eq(Productprocessresource::getProcessNo, dto.getProcessNo())
                        .eq(Productprocessresource::getProcessId, dto.getProcessId())));
                // 1.先找出id的父亲
//                Resourcegroup one1 = resourcegroupService.getOne(new LambdaQueryWrapper<Resourcegroup>().eq(Resourcegroup::getResourceGroupId, one.getResourceGroupParentId()));
//                if(one1 != null) {
//
//                }
            }
        } else {
            list = mapper.selPPRList(dto);
        }


//        LambdaQueryWrapper<Productprocessresource> wrapper = new LambdaQueryWrapper<>();
//        if(StringUtils.isEmpty(dto.getProductId())) wrapper.isNull(Productprocessresource::getProductId);
//        else wrapper.eq(Productprocessresource::getProductId, dto.getProductId());
//        if(StringUtils.isEmpty(dto.getProcessId())) wrapper.isNull(Productprocessresource::getProcessId);
//        else wrapper.eq(Productprocessresource::getProcessId, dto.getProcessId());
//        if(StringUtils.isEmpty(dto.getProcessNo())) wrapper.isNull(Productprocessresource::getProcessNo);
//        else wrapper.eq(Productprocessresource::getProcessNo, dto.getProcessNo());
//        List<Productprocessresource> list = this.list(wrapper);
//        for(Productprocessresource t : list) {
//            Resourcegroup one = resourcegroupService.getOne(new LambdaQueryWrapper<Resourcegroup>()
//                    .eq(Resourcegroup::getResourceGroupId, t.getResourceGroupId()));
//            t.setResourceGroupNO(one.getResourceGroupNO());
//            t.setResourceGroupName(one.getResourceGroupName());
//        }
        return list;
    }

}
